package com.detrum.detrumdx.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.detrum.base.BaseStatic;
import com.detrum.base.ServiceStatic;
import com.detrum.detrumdata.service.USBConnetService;
import com.detrum.detrumdx.R;
import com.detrum.utils.LogUtils;
import com.detrum.utils.MyMethod;

public class WelcomeActivity extends AppCompatActivity {
    private final String TAG = "WelcomeActivity";
    private Context mContext;
    private ImageView ivWelcomeBg;
    private Animation animation;
    private boolean isEnd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(ServiceStatic.isLaunchUsbService){
            Intent intent = new Intent(this, USBConnetService.class);
            stopService(intent);
        }else{
            if(BaseStatic.isMainLaunch){
                MyMethod.toActivity(this,MainActivity.class,new String[]{"flag"},new String[]{"welcome"});
                finish();
            }
        }
        setContentView(R.layout.activity_welcome);
        mContext = this;
        initView();
    }

    private void initView() {
        ivWelcomeBg = findViewById(R.id.iv_welcome_bg);
        animation = AnimationUtils.loadAnimation(this, R.anim.welcome_bg);
        animation.setFillAfter(true);
        animation.setStartOffset(500);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                LogUtils.i(TAG+"  onAnimationStart");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                LogUtils.i(TAG +"   onAnimationEnd");
                isEnd = true;
                Intent intent = new Intent(mContext, MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        ivWelcomeBg.startAnimation(animation);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isEnd){
            Intent intent = new Intent(mContext, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        animation.cancel();
    }
}
